// ------------------------------------------------------------------------------------------------
// File   : DX9AfterEffectManager.h
// Date   : 2012-08-01
// Author : Weng xiao yi
// Descr. : 
//
// ------------------------------------------------------------------------------------------------

#ifndef OY_DX9AFTEREFFECTMANAGER_H
#define OY_DX9AFTEREFFECTMANAGER_H

#include "DX9AfterEffect.h"

enum EAfterEffectType
{
	AE_Bloom,
	AE_PostFog,
	AE_DOF,
	AE_FXAA,
	AE_HeadLight,
	AE_Gamma,
	AE_Num_Effects
};

// ------------------------------------------------------------------------------------------------
// CLASSES
// ------------------------------------------------------------------------------------------------

class DX9AfterEffectManager
{
public:

	DX9AfterEffectManager(void);
	~DX9AfterEffectManager(void);

	inline static DX9AfterEffectManager* GetInstance(void) 
	{ 
		static DX9AfterEffectManager sm_Instance;
		return &sm_Instance;
	}

	void Initialize(LPDIRECT3DDEVICE9 pDevice);
	void Shutdown(void);
	void OnDeviceLost(void);
	void OnDeviceReset(void);

	void ApplyEffects(void);
	void BeginRender(void);

	// after effect params access
	inline BOOL  IsEnabled(ULONG _ulAENum,                          DX9_AE_Context _eContext = eDX9_AE_Context_Write);
	inline void  Enable   (ULONG _ulAENum, BOOL _bEnabled,          DX9_AE_Context _eContext = eDX9_AE_Context_Write);
	inline FLOAT GetParam (ULONG _ulAENum, ULONG _ulP1,             DX9_AE_Context _eContext = eDX9_AE_Context_Write);
	inline void  SetParams(ULONG _ulAENum, ULONG _ulP1, float _fP1, DX9_AE_Context _eContext = eDX9_AE_Context_Write);

	DX9AfterEffect* GetAfterEffect( EAfterEffectType AEType ) 
	{ 
		if( AEType < AE_Num_Effects )
			return m_apoEffects[AEType];
		return NULL;
	}

	LPDIRECT3DDEVICE9	m_pDevice;			   // D3D device

private:

	DX9AfterEffect*     m_apoEffects[AE_Num_Effects];

};

#define DX9GetAfterEffectManager() DX9AfterEffectManager::GetInstance()

// ------------------------------------------------------------------------------------------------
// INLINE IMPLEMENTATION
// ------------------------------------------------------------------------------------------------

inline BOOL DX9AfterEffectManager::IsEnabled(ULONG _ulAENum, DX9_AE_Context _eContext)
{
	DX9Verify(_ulAENum < AE_Num_Effects);

	return ((m_apoEffects[_ulAENum] != NULL) && (m_apoEffects[_ulAENum]->IsEnabled(_eContext)));
}

inline void DX9AfterEffectManager::Enable(ULONG _ulAENum, BOOL _bEnabled, DX9_AE_Context _eContext)
{
	DX9Verify(_ulAENum < AE_Num_Effects);

	if(m_apoEffects[_ulAENum] != NULL)
		m_apoEffects[_ulAENum]->Enable(_eContext, _bEnabled);
}

inline float DX9AfterEffectManager::GetParam(ULONG _ulAENum, ULONG _ulP1, DX9_AE_Context _eContext)
{
	DX9Verify(_ulAENum < AE_Num_Effects);

	if(m_apoEffects[_ulAENum] != NULL)
	{
		return m_apoEffects[_ulAENum]->GetParam(_eContext, _ulP1);
	}

	return 0.0f;
}

inline void DX9AfterEffectManager::SetParams(ULONG _ulAENum, ULONG _ulP1, float _fP1, DX9_AE_Context _eContext)
{
	DX9Verify(_ulAENum < AE_Num_Effects);

	if(m_apoEffects[_ulAENum] != NULL)
	{
		m_apoEffects[_ulAENum]->SetParam(_eContext, _ulP1, _fP1);
	}
}

#endif // #ifdef OY_DX9AFTEREFFECTMANAGER_H
